package recepteur;
//Recepteur bluetooth coté brique : Algos de pilotage de la brique (esclave)


import java.awt.*;
import java.io.*;

import lejos.nxt.*;
import lejos.nxt.Button;
import lejos.nxt.comm.*;
import lejos.robotics.RegulatedMotorListener;

public class Recepteur implements ButtonListener, SensorPortListener {
	
	
	public void stateChanged (SensorPort sp ,int ancien, int nouveau ){
		TouchSensor pareChoc = new TouchSensor(SensorPort.S1);
		//un Thread pour gérer le capteur qui sert de pare-choc;
		if (pareChoc.isPressed() ){
			for (int i=1; i<2; i++){
			affLCD("ATTENTION \nAU \nMUR !\n"+sp.readValue(), 0, 2);
			
			
			try {
				
				Sound.pause(500);
				demiTour();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			}
		}
		attendre();
		
	}
	public void affLCD (String str, int x, int y){
		
		LCD.clear();
		LCD.drawString(str, x, y);
		
	}
	
	public void attendre(){
		Motor.A.stop(); Motor.A.resetTachoCount();
		Motor.B.stop(); Motor.B.resetTachoCount();
		Sound.pause(200);
		
		affLCD ("Attente d\'\ninstructions.", 0,0);
		
		
	}
	
	public void demiTour() throws InterruptedException {
			
		Motor.B.forward();
		Motor.A.forward();
		affLCD ("On l\'a \néchappé \nbelle !", 0,0);
		int cpt=0;
		while (cpt<720){
			cpt=Motor.A.getTachoCount();
		}
		Thread.sleep(1000);
		Motor.B.resetTachoCount();
		Motor.B.forward();
		Motor.A.stop();
		int demiTour=0;
		while(demiTour < 900){
			demiTour=Motor.B.getTachoCount();
		}
		arreter();
	}
	
	
	public void avancer() throws InterruptedException {
		Motor.A.setSpeed(360);
		Motor.A.backward();
		Motor.B.setSpeed (360);
    	Motor.B.backward();
    	
    	affLCD ("An Avant !", 0,0);
	}
	
	public void tournerAGauche() throws InterruptedException{
		
		Motor.A.setSpeed(360/2);
		Motor.B.setSpeed(360/2);
		Motor.B.stop(true);
		
		Motor.A.backward();
		
		affLCD ("A Gauche...", 0,0);
	
		
	}
	public void tournerADroite() throws InterruptedException{
		
		Motor.A.setSpeed(360/2);
		Motor.B.setSpeed(360/2);
		Motor.A.stop(true);
		Motor.B.backward();
		
		affLCD ("A Droite...", 0,0);
		
	}
	public void arreter() throws InterruptedException{
		Motor.B.stop(); Motor.B.resetTachoCount();
		Motor.A.stop();	Motor.A.resetTachoCount();
		
		affLCD ("Stop", 0,0);
		
	}
	
	
   public Recepteur() throws AWTException, InterruptedException, IOException {
       Button.ESCAPE.addButtonListener(this);
       SensorPort.S1.addSensorPortListener(this);
       
      // Robot robot = new Robot();
              
       LCD.drawString("Attente de \nconnexion...", 0, 0);
       NXTConnection connexion = Bluetooth.waitForConnection();
       DataInputStream requete = connexion.openDataInputStream();
       DataOutputStream reponse = connexion.openDataOutputStream();
       
       LCD.drawString("Connexion OK", 0, 3);
           	   
    	   
           
           attendre(); //en attendant une commande
           int pilot = 0;
           do {
 
        	   pilot = requete.read(); //on recoit le DataOutputStream du PC
        	   int rep; //pour la réponse à renvoyer vers le PC de puis la brique
           
        	           	   
           if (pilot == 1){
        	   avancer();  rep =pilot;
        	   LCD.drawString("Type : "+pilot, 0, 4);
            
        	   reponse.write(rep);reponse.flush();
        	   
           }
           if (pilot == 2){
        	   tournerAGauche();  rep =pilot;
        	   LCD.drawString("Type : "+pilot, 0, 4);
            
        	   reponse.write(rep);reponse.flush();
           }
           if (pilot == 3){
        	   tournerADroite();  rep =pilot;
        	   LCD.drawString("Type : "+pilot, 0, 4);
            
        	   reponse.write(rep);reponse.flush();
           }
           if (pilot == 4){
        	   arreter(); rep= pilot;        	   
        	   LCD.drawString("Type : "+pilot, 0, 4);
               reponse.write(rep);reponse.flush();
           }
           }
    	   while (pilot != 0) ;
            
    	   arreter();
    	       
           finConnexion(requete, reponse, connexion);
              
     
           }
   
   
   public static void main(String[] args) throws AWTException, InterruptedException, IOException { 
       new Recepteur();
   }
   public void finConnexion (DataInputStream requete, DataOutputStream reponse, NXTConnection connexion) throws IOException{
	   Sound.pause(4000);
       
       //on libere les ressources si l'ordre est ==0
       requete.close();
       reponse.close();
       connexion.close();
   }


	
	public void buttonPressed(Button b) {
		// TODO Auto-generated method stub
		affLCD("Au Revoir", 0, 0);
		System.exit(0);
	}

	public void buttonReleased(Button b) {
		// TODO Auto-generated method stub
		
	}

}